package com.tianshi.product.mapper;


import com.tianshi.entity.Products;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {


    @Select("SELECT * FROM products WHERE product_id = #{productId}")
    Products getProductById(@Param("productId")Long id );


    @Select("SELECT * FROM products")
    List<Products> getAllProducts();

    @Select("SELECT * FROM products WHERE product_name LIKE CONCAT('%',#{productName},'%')")
    List<Products> searchProductByName(@Param("productName")String productName);

    @Insert("INSERT INTO products(product_name, description,image,price,stock,category_id,status,created_at,updated_at) " +
            "VALUES(#{productName},#{description},#{image},#{price},#{stock},#{categoryId},#{status},#{createdAt},#{updatedAt})")
    Long addProduct(Products product);

//    @Update("UPDATE products SET product_name = #{productName}, description = #{description},image = #{image},price = #{price},stock = #{stock},category_id = #{categoryId} WHERE product_id = #{productId}")
    Long updateProduct(Products product);

    @Delete("DELETE FROM products WHERE product_id = #{productId}")
    Long deleteProduct(@Param("productId")Long id);


    //根据关键字搜索商品
    @Select("SELECT * FROM products WHERE product_name LIKE CONCAT('%',#{keyword},'%')")
    List<Products> searchByKeyword(String keyword);
    //根据分类搜索商品
    @Select("SELECT * FROM products WHERE category_id = #{categoryId}")
    List<Products> searchByCategory(Long categoryId);
    //获取热门商品
    @Select("SELECT * FROM products WHERE status =1 AND stock > 0 LIMIT #{num}")
    List<Products> getHotProducts(int num);
}
